Passed
Pull Request — master (#26)
by lv
02:01
created

module.exports   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 1
c 0
b 0
f 0
nc 2
rs 9.3333
nop 2
1
const Redis = require('./libraries/redis')
0 ignored issues
show
Unused Code introduced by
The constant Redis seems to be never used. Consider removing it.
Loading history...
2
const Constant = require('./libraries/constant')
0 ignored issues
show
Unused Code introduced by
The constant Constant seems to be never used. Consider removing it.
Loading history...
3
const ApiError = require('./util/api_error')
0 ignored issues
show
Unused Code introduced by
The constant ApiError seems to be never used. Consider removing it.
Loading history...
4
const _ = require('underscore')
0 ignored issues
show
Unused Code introduced by
The constant _ seems to be never used. Consider removing it.
Loading history...
5
6
module.exports = function (permission) {
7
8
	return async function (ctx, next) {
0 ignored issues
show
Bug introduced by
The variable async seems to be never declared. If this is a global, consider adding a /** global: async */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
10
		async function checkToken() {
0 ignored issues
show
Bug introduced by
The variable async seems to be never declared. If this is a global, consider adding a /** global: async */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
11
			let token = (typeof (ctx.request.headers.token) == 'undefined' || !ctx.request.headers.token) ?
12
				ctx.cookies.get('token') : ctx.request.headers.token
13
			let uid = (typeof (ctx.request.headers.uid) == 'undefined' || !ctx.request.headers.uid) ?
14
				ctx.cookies.get('uid') : ctx.request.headers.uid
15
16
			if (!token || !uid) {
17
				console.log('token: ' + token)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
18
				console.log('uid: ' + uid)
19
				throw new ApiError('auth.error', 'token missing')
0 ignored issues
show
Bug introduced by
The variable ApiError seems to be never declared. If this is a global, consider adding a /** global: ApiError */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
20
			}
21
22
			sessionKey = Constant.CATERING_SESSION + token
0 ignored issues
show
Bug introduced by
The variable sessionKey seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.sessionKey.
Loading history...
Bug introduced by
The variable Constant seems to be never declared. If this is a global, consider adding a /** global: Constant */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
23
			session = await Redis.get(sessionKey)
0 ignored issues
show
Bug introduced by
The variable Redis seems to be never declared. If this is a global, consider adding a /** global: Redis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable session seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.session.
Loading history...
24
			session = JSON.parse(session)
25
			if (!session) {
26
				throw new ApiError('auth.error', 'token error')
27
			}
28
29
			if (session.uid == uid) {
30
				ctx.uid = uid
31
				return true
32
			} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
33
				throw new ApiError('auth.error', 'no permission')
34
			}
35
			
36
		}
37
38
		async function checkUser() {
0 ignored issues
show
introduced by
The function checkUser does not seem to be used and can be removed.
Loading history...
39
			await checkToken()
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
			await next()
41
		}
42
43
		// 检查header
44
		if (!_.has(ctx.request.headers, 'store-id')) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
45
			throw new ApiError('validate.error', 'store-id')
0 ignored issues
show
Bug introduced by
The variable ApiError seems to be never declared. If this is a global, consider adding a /** global: ApiError */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
		}
47
		if (!_.has(ctx.request.headers, 'mina-source')) {
48
			throw new ApiError('validate.error', 'mina-source')
49
		}
50
51
		// guest
52
		if (permission === 'guest') {
53
			await next()
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
54
		} else if (permission === 'user') {
55
			return await checkUser()
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
56
		} else {
57
			throw new ApiError('role.notExist')
58
		}
59
60
	}
61
62
}
63